package ru.spbau.lifeincube.swing;


import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SettingsDialog extends JDialog {

    private JLabel mXSizeText;
    private JLabel mYSizeText;
    private JLabel mZSizeText;

    private JLabel mToDieText;
    private JLabel mToLiveText;
    private JLabel mToBornText;
    private JButton mSaveBtn;
    
    private BaseFrame mBaseFrame;

    public SettingsDialog(BaseFrame owner) {
        super(owner);
        mBaseFrame = owner;
        Box box = Box.createVerticalBox();
        mSaveBtn = new JButton("Save");
        box.add(new JLabel("Size"));

        mXSizeText = new JLabel();
        mYSizeText = new JLabel();
        mZSizeText = new JLabel();
        box.add(createSliderBox("X", mXSizeText, 3, 30,10));
        box.add(createSliderBox("Y", mYSizeText, 3, 30,10));
        box.add(createSliderBox("Z", mZSizeText, 3, 30,10));


        box.add(new JLabel("Game"));
        mToBornText = new JLabel();
        mToDieText = new JLabel();
        mToLiveText = new JLabel();
        box.add(createSliderBox("To Die ", mToDieText, 0, 26,3));
        box.add(createSliderBox("To Live", mToLiveText, 0, 26,10));
        box.add(createSliderBox("To Born", mToBornText, 0, 26,4));

        box.add(mSaveBtn);
        mSaveBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                mBaseFrame.refresh();
            }
        });
        Dimension minSize = new Dimension(200, 300);
        setMinimumSize(minSize);
        setContentPane(box);
    }

    private Box createSliderBox(String name, final JLabel valueLable, int from, int to,int def) {
        Box box = Box.createHorizontalBox();
        JLabel title = new JLabel(name);
        box.add(title);
        final JSlider slider = new JSlider(from, to);
        slider.setValue(def);
        slider.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                valueLable.setText(slider.getValue() + "");
            }
        });

        box.add(slider);
        valueLable.setText(slider.getValue() + "");
        box.add(valueLable);
        Border border = new EmptyBorder(4, 4, 4, 4);
        box.setBorder(border);
        return box;
    }


    public int getXValue() {
        return Integer.valueOf(mXSizeText.getText());
    }

    public int getYValue() {
        return Integer.valueOf(mYSizeText.getText());
    }

    public int getZValue() {
        return Integer.valueOf(mZSizeText.getText());
    }

    public int getToLiveValue() {
        return Integer.valueOf(mToLiveText.getText());
    }

    public int getToDieValue() {
        return Integer.valueOf(mToDieText.getText());
    }

    public int getToBornValue() {
        return Integer.valueOf(mToBornText.getText());
    }

}
